In this notebook, we will implement matlab imfilter method in python. Here we will implement four modes know as - (clip, wrap, copy, reflect) in old_matlab, (0, circular, replicate, symmetic) in new_matlab and (constant,wrap and mirror, nearest, reflect) in scipy python.
In [1]:
import cv2
import numpy as np
import matplotlib.pyplot as plt
import scipy.ndimage as scp
In [2]:
img = cv2.imread('paint.jpg', cv2.IMREAD_GRAYSCALE)
In [3]:
kernal = np.zeros((51,51))
kernal[25,25] = 1
Constant_filter = scp.correlate(img,kernal,mode='constant')
Wrap_filter = scp.correlate(img,kernal,mode='wrap')
Mirror_filter = scp.correlate(img,kernal,mode='mirror')
Nearest_filter = scp.correlate(img,kernal,mode='nearest')
Reflect_filter = scp.correlate(img,kernal,mode='reflect')
plt.figure(figsize=(10,8))
plt.subplot(2,3,1), plt.imshow(img, cmap='gray'), plt.title('Original')
plt.subplot(2,3,2), plt.imshow(Constant_filter, cmap='gray'), plt.title('Constant_filtered')
plt.subplot(2,3,3), plt.imshow(Wrap_filter, cmap='gray'), plt.title('Wrap_filtered')
plt.subplot(2,3,4), plt.imshow(Mirror_filter, cmap='gray'), plt.title('Mirror_filtered')
plt.subplot(2,3,5), plt.imshow(Nearest_filter, cmap='gray'), plt.title('Nearest_filtered')
plt.subplot(2,3,6), plt.imshow(Reflect_filter, cmap='gray'), plt.title('Reflect_filtered')
plt.show()